<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="中文名称" prop="name">
          <el-input
            v-model="queryParams.chineseName"
            placeholder="请输入中文名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="版本号" prop="version">
          <el-select v-model="queryParams.version" placeholder="请选择版本号" clearable>
            <el-option
              v-for="dict in dict.type.report_version"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['reportworld:description:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['reportworld:description:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['reportworld:description:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['reportworld:description:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

      <el-table border="border" max-height="550px" v-loading="loading" :data="descriptionList"
                @selection-change="handleSelectionChange" @row-click="rowClick"
                @sort-change="handleSortChange" :row-style="tableRowStyle" ref="description">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column
          v-hasPermi="['reportworld:description:query']"
          label="操作" align="center" width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleUpdate(scope.row,false)"
              v-hasPermi="['reportworld:description:query']"
            >详情
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" prop="name" width="180" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="中文名称" align="center" prop="chineseName" width="180" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="内容信息" align="center" prop="description" width="180" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="显示类型" align="center" prop="typ" width="180" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="版本号" align="center" prop="version" width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.report_version" :value="scope.row.version"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

      <!-- 添加或修改报表世界说明信息对话框 -->
      <el-dialog v-if="open" :close-on-click-modal ="false" :title="title" :visible.sync="open" width="1200px"   append-to-body >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input clearable v-model="form.name" placeholder="请输入名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="中文名称" prop="chineseName">
                <el-input  clearable v-model="form.chineseName" placeholder="请输入中文名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内容信息" prop="description">
                <monaco-editor ref="monacoEditor"
                               style="border:1px solid #000;"
                               :opts="opts"></monaco-editor>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="类型" prop="type">
                <el-select clearable v-model="form.type" placeholder="请选类型">
                  <el-option
                    v-for="dict in types"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="版本号" prop="version">
                <el-select clearable v-model="form.version" placeholder="请选择版本号">
                  <el-option
                    v-for="dict in dict.type.report_version"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_common_status"
                    :key="dict.value"
                    :label="parseInt(dict.value)"
                  >{{dict.label}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <textarea  v-model="form.remark" style="width:100%;height: 200px" placeholder="请输入备注"></textarea>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="isUpdate" type="primary" :loading="butloading" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <test></test>
    </div>
</template>

<script>
    import { listDescription, getDescription, delDescription, addDescription, updateDescription } from "@/api/reportworld/description";
    import { getTableRowStyle } from "@/utils/yss";
    import monacoEditor from '@/components/SqlEditor';

    export default {
        name: "Description",
         dicts: ['sys_common_status', 'report_version'],
      components: {
        monacoEditor
      },
        data() {
          const equalToDescription = (rule, value, callback) => {
            if (this.$refs.monacoEditor.getVal()) {
              callback()
            } else {
              callback(new Error("内容信息不能为空"));
            }
          };
          return {
            butloading:false,
            isUpdate: false,
            types: [
              {
                value: "sql",
                label: "sql"
              },
              {
                value: "json",
                label: "json"
              },
              {
                value: "text",
                label: "text"
              }
            ],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 报表世界说明信息表格数据
            descriptionList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
              pageNum: 1,
              pageSize: 50,
              chineseName: null,
              name: null,
              description: null,
              version: null,
              status: null,
            },
          opts: { //编辑器
            value: '',
            readOnly: false, // 是否可编辑
            language: 'text', // 语言类型
            theme: 'vs', // 编辑器主题
            lineNumbers: "on", // 行号 取值
          },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
              name: [
                { required: true, message: "名称不能为空", trigger: "blur" }
              ],
              chineseName: [
                { required: true, message: "中文名称不能为空", trigger: "blur" }
              ],
              description: [
                {
                  required: true,
                  validator: equalToDescription,
                  message: "内容信息不能为空",
                  trigger: ["blur"]
                }
              ]
            }
        };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询报表世界说明信息列表 */
            getList() {
                this.loading = true;
                listDescription(this.queryParams).then(response => {
                this.descriptionList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
              this.form = {
                id: null,
                chineseName: null,
                description: null,
                version: null,
                status: null,
                createTime: null,
                createBy: null,
                updateTime: null,
                updateBy: null,
                remark: null
              };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 排序触发事件 */
            handleSortChange(column, prop, order) {
                this.queryParams.orderByColumn = column.prop;
                this.queryParams.isAsc = column.order;
                this.getList();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加报表世界说明信息";
                this.isUpdate =true;
                this.form.version = this.dict.type.report_version[0].value;
                this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
                this.form.type = "text";
            },
            /** 修改按钮操作 */
            handleUpdate(row,isUpdate) {
                this.reset();
                const id = row.id || this.ids
                getDescription(id).then(response => {
                this.form = response.data;
                this.opts.language= response.data.type;
                this.opts.value= response.data.description;
                this.open = true;
                this.title = "修改报表世界说明信息";
              this.isUpdate =true;
              if(isUpdate == false){
                this.title = "说明信息详情";
                this.isUpdate =false;
              }
            });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                      this.butloading=true;
                       this.form.description = this.$refs.monacoEditor.getVal();
                        if (this.form.id != null) {
                            updateDescription(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        } else {
                            addDescription(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除数据项？').then(function() {
                    return delDescription(ids);
                }).then(() => {
                    this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
            },
    /** 导出按钮操作 */
    handleExport() {
        this.download('reportworld/description/export', {
                ...this.queryParams
    }, `description_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    /** 单击选中行数据 */
    rowClick(row) {
        this.$refs.description.toggleRowSelection(row);
    },
    }
    };
</script>
